import { func } from 'vue-types';

const crypto = require('crypto');
const CryptoJS = require('crypto-js');  //引用AES源码js
const key = CryptoJS.enc.Utf8.parse("JId2z7TCRB8gpZC8");  //十六位十六进制数作为密钥
const iv = CryptoJS.enc.Utf8.parse('5PVnx4VzsPwUR8aH');   //十六位十六进制数作为密钥偏移量
//解密方法
export function Decrypt(word) {
    let decrypt = CryptoJS.AES.decrypt(word, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    })
    // console.log(decrypt)
    let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8)
    return decryptedStr.toString()
}
//加密方法
export function Encrypt(word) {
    let encrypted = CryptoJS.AES.encrypt(word, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    })
    return encrypted.toString();
}

//密钥签名加密
export function HmacMD5(text)
{
    const crypto = require('crypto');
    // 创建一个hmac对象,参数一为加密类型，参数2为KEY
    const hmac = crypto.createHmac('md5', 'Y3Dl0NaP7CWEU9Ur');
    // 往hmac对象中添加加密内容
    const up = hmac.update(text);
    // 使用 digest 方法输出加密后内容
    const result = up.digest('hex').toUpperCase();
    return result;
}

export function AesMain(params){
    var strParams = Encrypt(JSON.stringify(params))
    const data = {
		Data:strParams,
		OpenID:'SEIS634356',
		Sig:HmacMD5('SEIS634356'+strParams)
	}
    return data
}
